package com.app.mvc.action.corndbaction;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Paint;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYStepRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.RectangleInsets;
import org.jfree.ui.TextAnchor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.app.service.ICornDBExcelService;
import com.app.service.ICornDBService;
import com.app.util.DNAUtil;


@Controller
@RequestMapping(value="/charts")
public class JfreeChartAction {
	
	
	@RequestMapping(value="/piechart",method=RequestMethod.GET)
	public void drawChart(HttpServletResponse response,HttpServletRequest request){
		response.setContentType("image/png");
		String p = request.getParameter("p");
		String s1 = request.getParameter("s1");
		String s2 = request.getParameter("s2");
		String s3 = request.getParameter("s3");
		String s4 = request.getParameter("s4");
		double b1 = 0, b2 = 0,b3=0,b4=0;
		try{
			b1 = Double.parseDouble(s1==null?"0":s1);
			b2 = Double.parseDouble(s2==null?"0":s2);
			b3 = Double.parseDouble(s3==null||"undefined".equals(s3)?"0":s3);
			b4 = Double.parseDouble(s4==null||"undefined".equals(s4)?"0":s4);
			
		
		}catch(Exception ex){
			ex.printStackTrace();
		}int width=200;
		boolean b= false;
		if(b3>0||b4>0){
			width=400;
			b=true;
		}	
		JFreeChart localJFreeChart = createChart(b1,b2,p,b3,b4,b);	
		
		try{
			ChartUtilities.writeChartAsPNG(response.getOutputStream(), localJFreeChart,width, 300);
			response.getOutputStream().close();
		}catch(IOException ex){
			ex.printStackTrace();
		}
		
	}
	
	private static JFreeChart createChart(double d1,double d2,String str,double d3,double d4,boolean b)
	  {
		
		XYSeries localXYSeries1 = new XYSeries("1-1");
	    localXYSeries1.add(-2.0D, d1);
	    localXYSeries1.add(-1.0D, d1);
	    
	    XYSeries localXYSeries2 = new XYSeries("1-2");
	    localXYSeries2.add(-2.0D, d2);
	    localXYSeries2.add(-1.0D, d2);
	    XYSeriesCollection paramXYDataset = new XYSeriesCollection();
	    paramXYDataset.addSeries(localXYSeries1);
	    paramXYDataset.addSeries(localXYSeries2);
	    if(b){
	    	XYSeries localXYSeries3 = new XYSeries("2-1");
	 	    localXYSeries3.add(1.0D, d3);
	 	    localXYSeries3.add(2.0D, d3);
	 	    paramXYDataset.addSeries(localXYSeries3);
	 	    XYSeries localXYSeries4 = new XYSeries("2-2");
	 	    localXYSeries4.add(1.0D, d4);
	 	    localXYSeries4.add(2.0D, d4);
	 	    paramXYDataset.addSeries(localXYSeries4);
	    	str+=" 标准                       待测";
	    }
		JFreeChart localJFreeChart = ChartFactory.createXYLineChart("", str, "", paramXYDataset, PlotOrientation.VERTICAL, true, true, false);
		TextTitle title = new TextTitle("点位图", new Font("宋体", Font.BOLD, 20));
		localJFreeChart.setTitle(title); 
		
		   //解决图表底部中文乱码问题  
		localJFreeChart.getLegend().setVisible(false);
		localJFreeChart.getLegend().setItemLabelPadding(RectangleInsets.ZERO_INSETS);
		localJFreeChart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
		 
	
	    XYPlot localXYPlot = (XYPlot)localJFreeChart.getPlot();
	    localXYPlot.setDomainPannable(true);
	    localXYPlot.setRangePannable(false);

	    if(b){
		    localXYPlot.setDomainZeroBaselineVisible(true);
		    localXYPlot.setRangeZeroBaselineVisible(false);
	    }else{
	    	localXYPlot.setDomainZeroBaselineVisible(false);
		    localXYPlot.setRangeZeroBaselineVisible(false);
	    }
	    if(b){
	    	localXYPlot.getDomainAxis().setRange(-3, 3);
	    }else{
	    	localXYPlot.getDomainAxis().setRange(-3, 0);
	    }
	    localXYPlot.getDomainAxis().setVisible(true); //隐藏x轴值
	    localXYPlot.getDomainAxis().setTickLabelsVisible(false);
	    localXYPlot.getDomainAxis().setLabelFont(new Font("宋体", Font.PLAIN, 12));
	    ValueAxis axis = localXYPlot.getRangeAxis() ;
	    double y = 8.5;
	    if(d1>8.0||d2>8.0||d3>8.0||d4>8.0){
	    	double[] dd=new double[]{d1,d2,d3,d4};
	    	double temp = 0.0;	
	    	   for (int i = 0; i < 3; i++) {  
	    	        for (int j = i + 1; j < 4; j++) {  
	    	            if (dd[i] < dd[j]) { 
	    	                temp = dd[i];  
	    	                dd[i] = dd[j];  
	    	                dd[j] = temp;  
	    	            }  
	    	        }  
	    	    }   
    	   y=Math.round(dd[0])+0.5;
	    }
	    axis.setRange(-0.5,y) ; //设置Y轴值范围
	    localXYPlot.setRangeAxis(axis);
	   
	    XYTextAnnotation localXYTextAnnotation = null;
	    if(d1!=d2){
		    localXYTextAnnotation = new XYTextAnnotation("通道1", -0.9D, d1);
		   // localXYTextAnnotation.setFont(localFont);
		    localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
		    localXYPlot.addAnnotation(localXYTextAnnotation);
		    
		    localXYTextAnnotation = new XYTextAnnotation("通道2", -0.9D, d2);
		   // localXYTextAnnotation.setFont(localFont);
		    localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
		    localXYPlot.addAnnotation(localXYTextAnnotation);
	    }else{
	    	 localXYTextAnnotation = new XYTextAnnotation("通道1、2", -0.9D, d1);
			   // localXYTextAnnotation.setFont(localFont);
			 localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
			 localXYPlot.addAnnotation(localXYTextAnnotation);	    	
	    }
	    
	    
	    Paint[] paint = new Paint[] {
	             Color.black,
                 Color.black,Color.black,Color.black};
	    if(b){
	    	if(d1!=d3){
	    		paint[1]=Color.red;
	    	}
	    	if(d2!=d4){
	    		paint[0]=Color.red;
	    	}
	    	
	    	if(d3!=d4){
	 		    localXYTextAnnotation = new XYTextAnnotation("通道1", 2.1D, d3);
	 		   // localXYTextAnnotation.setFont(localFont);
	 		    localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
	 		    localXYPlot.addAnnotation(localXYTextAnnotation);
	 		    
	 		    localXYTextAnnotation = new XYTextAnnotation("通道2", 2.1D, d4);
	 		   // localXYTextAnnotation.setFont(localFont);
	 		    localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
	 		    localXYPlot.addAnnotation(localXYTextAnnotation);
	 	    }else{
	 	    	 localXYTextAnnotation = new XYTextAnnotation("通道1、2", 2.1D, d3);
	 			   // localXYTextAnnotation.setFont(localFont);
	 			 localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
	 			 localXYPlot.addAnnotation(localXYTextAnnotation);	    	
	 	    }
	    }
	    
	    localXYPlot.setDrawingSupplier( new DefaultDrawingSupplier( paint, DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
                 DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
                 DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
                 DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));
	    
	    XYStepRenderer localXYStepRenderer = new XYStepRenderer();
	    localXYStepRenderer.setBaseShapesVisible(true);
	    localXYStepRenderer.setSeriesStroke(0, new BasicStroke(2.0F));
	    localXYStepRenderer.setSeriesStroke(1, new BasicStroke(2.0F));
	    localXYStepRenderer.setSeriesStroke(2, new BasicStroke(2.0F));
	    localXYStepRenderer.setSeriesStroke(3, new BasicStroke(2.0F));
	    localXYStepRenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
	    localXYStepRenderer.setDefaultEntityRadius(6);
	    localXYPlot.setRenderer(localXYStepRenderer);
	    return localJFreeChart;
	  }
	@RequestMapping(value="/piechartAll",method=RequestMethod.GET)
	public void drawChartAll(HttpServletResponse response,HttpServletRequest request){
		response.setContentType("image/png");
		String s1 = request.getParameter("i1");
		String s2 = request.getParameter("i2");
		int isexecl = 0;
		String sexecl = request.getParameter("isexecl");
		if(sexecl!=null&&!"".equals(sexecl)&&!"undefined".equals(sexecl)){
			isexecl= Integer.parseInt(sexecl);
		}
		if(s2==null||"undefined".equals(s2)){
			s2="";
		}
		int width=1100;
		boolean b= false;
		if(!"".equals(s2)){
			width=1100;
			b=true;
		}	
		JFreeChart localJFreeChart = createChart_(s1,s2,b,isexecl,corndbservice,cornDbExcelService);			
		try{
			ChartUtilities.writeChartAsPNG(response.getOutputStream(), localJFreeChart,width, 500);
			response.getOutputStream().close();
		}catch(IOException ex){
			ex.printStackTrace();
		}
		
	}
	@Autowired
	private ICornDBService corndbservice;
	@Autowired
	private ICornDBExcelService cornDbExcelService;
	
	private static JFreeChart createChart_(String s1,String s2,boolean b,int isexecl,ICornDBService corndbservice,ICornDBExcelService cornDbExcelService){
		List<Integer> data = null;
		if(isexecl==2){
			data = corndbservice.getCornDbDnaVoById(s1).getDnaValues();
		}else{
			data = cornDbExcelService.getById(s1).getDnaValues();
		}
		List<Integer> data_  =  new  ArrayList<Integer>();
		CollectionUtils.addAll(data_,  new  Object[data.size()]);
		Collections.copy(data_, data);
		List<Integer> data2 = null;
		List<Integer> data2_ = null;
		if(b){			
			data2 = cornDbExcelService.getById(s2).getDnaValues();
			data2_  =  new  ArrayList<Integer>();
			CollectionUtils.addAll(data2_,  new  Object[data2.size()]);
			Collections.copy(data2_, data2);
		}
		XYSeriesCollection paramXYDataset = new XYSeriesCollection();
		StringBuilder sb  = new StringBuilder();
		sb.append(".");
		for(int k=0;k<20;k++){
			if(k<9){
				sb.append("P0").append(k+1);
			}else{
				sb.append("P").append(k+1);
			}
			if(k%2==0){
				sb.append("     ");
			}else{
				sb.append("      ");
			}
		}
		JFreeChart localJFreeChart = ChartFactory.createXYLineChart("",sb.toString(), "", paramXYDataset, PlotOrientation.VERTICAL, true, true, false);
		TextTitle title = new TextTitle("点位图", new Font("宋体", Font.BOLD, 20));
		localJFreeChart.setTitle(title); 
		
		//解决图表底部中文乱码问题  
		localJFreeChart.getLegend().setVisible(false);
		localJFreeChart.getLegend().setItemLabelPadding(RectangleInsets.ZERO_INSETS);
		localJFreeChart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
		

	    XYPlot localXYPlot = (XYPlot)localJFreeChart.getPlot();
	    localXYPlot.setDomainPannable(true);
	    localXYPlot.setRangePannable(false);

	    if(b){
		    localXYPlot.setDomainZeroBaselineVisible(false);
		    localXYPlot.setRangeZeroBaselineVisible(false);
	    }else{
	    	localXYPlot.setDomainZeroBaselineVisible(false);
		    localXYPlot.setRangeZeroBaselineVisible(false);
	    }
	    if(b){
	    	localXYPlot.getDomainAxis().setRange(-40, 21);
	    }else{
	    	localXYPlot.getDomainAxis().setRange(-40, 1);
	    }
	    localXYPlot.getDomainAxis().setVisible(true); //隐藏x轴值
	    localXYPlot.getDomainAxis().setTickLabelsVisible(false);
	    localXYPlot.getDomainAxis().setLabelFont(new Font("宋体", Font.PLAIN, 12));
	    ValueAxis axis = localXYPlot.getRangeAxis() ;
	    double y = 8.5;
	    int _i = DNAUtil.getMax(data_);
	    if(_i>y){
	    	y=_i+0.5;
	    }
	    if(b){
	    	int __i = DNAUtil.getMax(data2_);
	    	if(__i>y){
		    	y=__i+0.5;
		    }
	    }
	    axis.setRange(-0.5,y) ; //设置Y轴值范围
	    localXYPlot.setRangeAxis(axis);
	    
	    Paint[] paint = new Paint[] {
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black,
	             Color.black,Color.black,Color.black,Color.black,Color.black};
			   
	    if(!b){
			for(int i=0;i<20;i++){
				String str = "P";
				if(i<9){
					str+="0";
				}
				XYSeries localXYSeries1 = new XYSeries(str+i);
			    localXYSeries1.add(-40.0D+i*2, data.get(i*2));
			    localXYSeries1.add(-39.0D+i*2, data.get(i*2));
			    XYSeries localXYSeries2 = new XYSeries(str+i+".");
			    localXYSeries2.add(-40.0D+i*2, data.get(i*2+1));
			    localXYSeries2.add(-39.0D+i*2, data.get(i*2+1));
			    paramXYDataset.addSeries(localXYSeries1);
			    paramXYDataset.addSeries(localXYSeries2);
			    XYTextAnnotation localXYTextAnnotation = null;
			    if( data.get(i*2)!=data.get(i*2+1)){
				    localXYTextAnnotation = new XYTextAnnotation("T1", -39.7D+i*2,  data.get(i*2)+0.4);
				   // localXYTextAnnotation.setFont(localFont);
				    localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
				    localXYPlot.addAnnotation(localXYTextAnnotation);
				    
				    localXYTextAnnotation = new XYTextAnnotation("T2",-39.7D+i*2, data.get(i*2+1)+0.4);
				   // localXYTextAnnotation.setFont(localFont);
				    localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
				    localXYPlot.addAnnotation(localXYTextAnnotation);
			    }else{
			    	 localXYTextAnnotation = new XYTextAnnotation("T1、2", -39.9D+i*2, data.get(i*2)+0.4);
					   // localXYTextAnnotation.setFont(localFont);
					 localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
					 localXYPlot.addAnnotation(localXYTextAnnotation);	    	
			    }
			    
			}
		}else{
			for(int i=0;i<20;i++){
				String str = "P";
				if(i<9){
					str+="0";
				}
				XYSeries localXYSeries1 = new XYSeries(str+i);
			    localXYSeries1.add(-40.0D+i*3, data.get(i*2));
			    localXYSeries1.add(-39.0D+i*3, data.get(i*2));
			    XYSeries localXYSeries2 = new XYSeries(str+i+".");
			    localXYSeries2.add(-40.0D+i*3, data.get(i*2+1));
			    localXYSeries2.add(-39.0D+i*3, data.get(i*2+1));
			    paramXYDataset.addSeries(localXYSeries1);
			    paramXYDataset.addSeries(localXYSeries2);
			    XYTextAnnotation localXYTextAnnotation = null;
			    if( data.get(i*2)!=data.get(i*2+1)){
				    localXYTextAnnotation = new XYTextAnnotation("T1", -39.8D+i*3,  data.get(i*2)+0.3);
				   // localXYTextAnnotation.setFont(localFont);
				    localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
				    localXYPlot.addAnnotation(localXYTextAnnotation);
				    
				    localXYTextAnnotation = new XYTextAnnotation("T2",-39.8D+i*3, data.get(i*2+1)+0.3);
				   // localXYTextAnnotation.setFont(localFont);
				    localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
				    localXYPlot.addAnnotation(localXYTextAnnotation);
			    }else{
			    	 localXYTextAnnotation = new XYTextAnnotation("T1、2", -40.0D+i*3, data.get(i*2)+0.3);
					   // localXYTextAnnotation.setFont(localFont);
					 localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
					 localXYPlot.addAnnotation(localXYTextAnnotation);	    	
			    }
			    
			    XYSeries localXYSeries3 = new XYSeries(str+i+"_");
			    localXYSeries3.add(-38.5D+i*3, data2.get(i*2));
			    localXYSeries3.add(-37.5D+i*3, data2.get(i*2));
			    XYSeries localXYSeries4 = new XYSeries(str+i+"_.");
			    localXYSeries4.add(-38.5D+i*3, data2.get(i*2+1));
			    localXYSeries4.add(-37.5D+i*3, data2.get(i*2+1));
			    paramXYDataset.addSeries(localXYSeries3);
			    paramXYDataset.addSeries(localXYSeries4);
			    if( data2.get(i*2)!=data2.get(i*2+1)){
				    localXYTextAnnotation = new XYTextAnnotation("T1", -38.4D+i*3,  data2.get(i*2)+0.2);
				   // localXYTextAnnotation.setFont(localFont);
				    localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
				    localXYPlot.addAnnotation(localXYTextAnnotation);
				    
				    localXYTextAnnotation = new XYTextAnnotation("T2",-38.4D+i*3, data2.get(i*2+1)+0.2);
				   // localXYTextAnnotation.setFont(localFont);
				    localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
				    localXYPlot.addAnnotation(localXYTextAnnotation);
			    }else{
			    	 localXYTextAnnotation = new XYTextAnnotation("T1、2", -38.6D+i*3, data2.get(i*2)+0.2);
					   // localXYTextAnnotation.setFont(localFont);
					 localXYTextAnnotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
					 localXYPlot.addAnnotation(localXYTextAnnotation);	    	
			    }
			    if(data.get(i*2)!=data2.get(i*2)){
		    		paint[80-(i+1)*4+1]=Color.red;
		    	}
		    	if(data.get(i*2+1)!=data2.get(i*2+1)){
		    		paint[80-(i+1)*4]=Color.red;
		    	}
		    	
		    	
			}
		}
	   

	    
	    localXYPlot.setDrawingSupplier( new DefaultDrawingSupplier( paint, DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
               DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
               DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
               DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));
	    
	    XYStepRenderer localXYStepRenderer = new XYStepRenderer();
	    localXYStepRenderer.setBaseShapesVisible(true);
	    for(int j=0;j<80;j++){
	    	 localXYStepRenderer.setSeriesStroke(j, new BasicStroke(2.0F));
	    }
	   
	    localXYStepRenderer.setBaseToolTipGenerator(new StandardXYToolTipGenerator());
	    localXYStepRenderer.setDefaultEntityRadius(6);
	    localXYPlot.setRenderer(localXYStepRenderer);
	    return localJFreeChart;
	  }

	public void setCorndbservice(ICornDBService corndbservice) {
		this.corndbservice = corndbservice;
	}

	public ICornDBService getCorndbservice() {
		return corndbservice;
	}

	public void setCornDbExcelService(ICornDBExcelService cornDbExcelService) {
		this.cornDbExcelService = cornDbExcelService;
	}

	
	
	
}
